package com.flag.liutao.modules.student.controller;


import com.flag.liutao.common.domain.IdParam;
import com.flag.liutao.common.domain.IdsParam;
import com.flag.liutao.common.domain.QueryParam;
import com.flag.liutao.common.domain.ResponseResult;


import com.flag.liutao.modules.student.param.GradeParam;
import com.flag.liutao.modules.student.service.IGradeService;
import com.flag.liutao.modules.student.vo.GradeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author godM
 * @since 2022-12-06
 */
@RestController
@RequestMapping("/admin/grade")
@Api(tags = "年级管理")
public class GradeController {

    @Autowired
    IGradeService gradeService;

    /**
     * 获取二级学院信息
     * @param param
     * @return
     */
    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys_power:grade_R')")
    @ApiOperation(value="分页获取年级信息",notes = "年级管理：分页获取年级信息")
    public ResponseResult<Map<String,Object>> getPage(QueryParam param){
        Map<String,Object> map = gradeService.getPage(param.getPage(),param.getLimit(),param.getKeyword());
        return ResponseResult.ok(map);
    }

    /**
     * 获取年级信息列表
     * @return
     */
    @GetMapping("list")
    @PreAuthorize("hasAuthority('sys_power:grade_R')")
    @ApiOperation(value="获取年级信息列表",notes = "年级管理：获取年级信息列表")
    public ResponseResult<List<GradeVo>> getPage(){
        return ResponseResult.ok(gradeService.getGradeList());
    }


    @PostMapping("add")
    @PreAuthorize("hasAuthority('sys_power:grade_C')")
    @ApiOperation(value="添加年级信息",notes = "年级管理：添加年级信息")
    public ResponseResult<Boolean> add(@Validated @RequestBody GradeParam param){
        gradeService.add(param);
        return ResponseResult.ok();
    }

    @PostMapping("edit")
    @PreAuthorize("hasAuthority('sys_power:grade_U')")
    @ApiOperation(value="修改年级信息",notes = "年级管理：修改年级信息")
    public ResponseResult<Boolean> edit(@Validated @RequestBody GradeParam param){
        gradeService.edit(param);
        return ResponseResult.ok();
    }

    @PostMapping("del")
    @PreAuthorize("hasAuthority('sys_power:grade_D')")
    @ApiOperation(value="删除年级信息",notes = "年级管理：删除年级信息")
    public ResponseResult<Boolean> del(@Validated @RequestBody IdParam param){
        gradeService.del(param.getId());
        return ResponseResult.ok();
    }

    @PostMapping("dels")
    @PreAuthorize("hasAuthority('sys_power:grade_D')")
    @ApiOperation(value="批量删除年级信息",notes = "年级管理：批量删除年级信息")
    public ResponseResult<Boolean> dels(@Validated @RequestBody IdsParam param){

        for (Long id : param.getIds()) {
            gradeService.del(id);
        }
        return ResponseResult.ok();
    }
}
